﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace OnlineMealOrdering.Common
{
    /// <summary>
    /// 
    /// </summary>
    public class ParseEnumeration
    {
        public static List<EnumItem> GetEnumerations(Type enumType)
        {

            if (enumType == null)
                throw new ArgumentNullException("enumType");

            if (!enumType.IsEnum)
                throw new ArgumentException("Type provided must be an Enum.\r\nParameter name: enumType");

            var enumValues = Enum.GetValues(enumType);

            var lst = new List<EnumItem>();

            foreach (var enumValue in enumValues)
            {
                lst.Add(new EnumItem(enumValue, GetEnumDisplayName(enumValue as Enum)));
            }

            return lst;
        }
        public static string GetEnumDisplayName(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }
        public class EnumItem
        {
            public Object Value { get; set; }

            public string DisplayName { get; set; }

            public string Description { get; set; }

            public EnumItem(Object status, string name)
            {
                this.Value = status;
                this.DisplayName = name;
            }
            public EnumItem(string name)
            {
                this.DisplayName = name;
            }

            /// <summary>
            /// return value of enum
            /// </summary>
            /// <returns></returns>
            public override string ToString()
            {
                return DisplayName;
            }
        }
                
    }
}
